package com.itheima.web;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Role;
import com.itheima.service.MenuService;
import com.itheima.service.RoleService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;


@RequestMapping("/role")
@RestController
public class RoleController {
    @Reference
    private RoleService roleService;
    @Reference
    MenuService menuService;

    @RequestMapping("/findAll")
    public Result findAll() {
        List<Role> data = roleService.findAll();
        return new Result(true, "", data);
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = roleService.findPage(queryPageBean);
        return pageResult;
    }
    @RequestMapping("/add")
    public Result add(@RequestBody Role role){
        try {
            roleService.add(role);
            return Result.OK("新增成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ERROR("新增失败");
        }
    }

    @RequestMapping("/findById4Edit")
    public Result findById4Edit(Integer id){
        Map map = roleService.findById4Edit(id);
        map.put("menus",menuService.findAll());
        return Result.OK("",map);
    }

    @RequestMapping("/edit")
    public Result eidt(@RequestBody Role role){
        try {
            roleService.edit(role);
            return Result.OK("编辑成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ERROR("编辑失败");
        }
    }
    @RequestMapping("/deleteById")
    public Result deleteById(Integer id){
        try {
            roleService.deleteById(id);
            return Result.OK("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.ERROR(e.getMessage());
        }
    }
}
